package com.tanhua.server.controller;

import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.domain.vo.Movements;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.VisitorsVo;
import com.tanhua.server.interceptor.UserThreadLocal;
import com.tanhua.server.service.MovementsService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("movements")
@RestController
public class MovementsController {

    @Autowired
    private MovementsService movementsService;

    /**
     * 发布动态
     */
    @PostMapping
    public ResponseEntity<Void> saveMovements(Publish publish, MultipartFile[] imageContent) throws IOException {
        publish.setUserId(UserThreadLocal.getUserId());
        movementsService.saveMovements(publish, imageContent);
        return ResponseEntity.ok(null);
    }


    /**
     * 查询好友的动态信息
     */
    @GetMapping
    public ResponseEntity<PageResult> queryPublishList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                       @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        page = page<1? 1: page;
        return this.movementsService.queryUserPublishList(page, pageSize);
    }

    /**
     * 查询推荐的动态信息
     */
    @GetMapping("recommend")
    public ResponseEntity<PageResult> queryRecommendPublishList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                                @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        page = page<1? 1: page;
        return this.movementsService.queryRecommendPublishList(page, pageSize);
    }

    /**
     * 查询单条动态信息
     */
    @GetMapping("{id}")
    public ResponseEntity<Movements> queryMovementsById(@PathVariable("id") String publishId){
        return this.movementsService.queryMovementsById(publishId);
    }


    /**
     * 点赞
     */
    @GetMapping("/{id}/like")
    public ResponseEntity<Long> likeComment(@PathVariable("id") String publishId) {
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(publishId));
        comment.setCommentType(1);
        comment.setPubType(1);
        comment.setUserId(UserThreadLocal.getUserId());
        return this.movementsService.saveComment(comment);
    }

    /**
     * 取消点赞
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity<Long> disLikeComment(@PathVariable("id") String publishId) {
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(publishId));
        comment.setCommentType(1);
        comment.setPubType(1);
        comment.setUserId(UserThreadLocal.getUserId());
        return this.movementsService.removeComment(comment);
    }

    /**
     * 喜欢
     */
    @GetMapping("/{id}/love")
    public ResponseEntity<Long> loveComment(@PathVariable("id") String publishId) {
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(publishId));
        comment.setCommentType(2);
        comment.setPubType(1);
        comment.setUserId(UserThreadLocal.getUserId());
        return this.movementsService.saveComment(comment);
    }

    /**
     * 取消喜欢
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity<Long> unLoveComment(@PathVariable("id") String publishId) {
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(publishId));
        comment.setCommentType(2);
        comment.setPubType(1);
        comment.setUserId(UserThreadLocal.getUserId());
        return this.movementsService.removeComment(comment);
    }

    /**
     * 自己的所有动态
     */
    @GetMapping("all")
    public ResponseEntity<PageResult> queryAlbumList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                     @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                                     @RequestParam(value = "userId") Long userId) {
        return this.movementsService.queryAlbumList(userId, page, pageSize);
    }


//
//
    /**
     * 谁看过我
     */
    @GetMapping("visitors")
    public ResponseEntity queryVisitorsList(){
        return this.movementsService.queryVisitorsList();
    }



}
